﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mesoBoard.Data;
using mesoBoard.Web.Core;
using mesoBoard.Services;

namespace mesoBoard.Web.Areas.Admin.Controllers
{
    public class ConfigsController : mesoBoardAdminController
    {
        [HttpPost]
        public ActionResult Config(string nothing)
        {
            Dictionary<string, string> ToUpdate = new Dictionary<string, string>();
            foreach (string f in Request.Form.Keys)
            {
                if (Repositories.Configs.IsConfig(f))
                {
                    if (string.IsNullOrEmpty(Request.Form[f]))
                        ModelState.AddModelError(f, f + " requires a value");

                    ToUpdate.Add(f, Request.Form[f]);
                }
            }
            if (ModelState.IsValid)
            {
                Repositories.Configs.BulkUpdate(ToUpdate);
                SetSuccess("Configs updated");
                SiteConfig.UpdateCache();
                Misc.ParseBBCodeScriptFile();
                return RedirectToAction("Config");
            }
            else
            {
                SetError("All fields are required, no values were updated");
                TempData["Errors"] = ModelState.Keys;
                return RedirectToAction("Config");
            }
        }

        [HttpGet]
        public ActionResult Config()
        {
            ViewData["BoardTheme"] = new SelectList(Repositories.Themes.GetThemes(), "ThemeID", "DisplayName", SiteConfig.BoardTheme.Value);
            SetBreadCrumb("Configuration");
            IEnumerable<Config> AllConfigs = Repositories.Configs.Configs();

            ViewData["Settings"] = AllConfigs.Select(x => x.Group).Distinct().ToArray();

            return View(AllConfigs);
        }

        [HttpPost]
        public ActionResult PluginConfig()
        {
            Dictionary<string, string> ToUpdate = new Dictionary<string, string>();
            foreach (string f in Request.Form.Keys)
            {
                if (Repositories.PluginConfigs.IsPluginConfig(f))
                {
                    if (string.IsNullOrEmpty(Request.Form[f]))
                        ModelState.AddModelError(f, f + " requires a value");

                    ToUpdate.Add(f, Request.Form[f]);
                }
            }
            if (ModelState.IsValid)
            {
                Repositories.PluginConfigs.BulkUpdate(ToUpdate);
                SetSuccess("PluginConfigs updated");
                return RedirectToAction("PluginConfig");
            }
            else
            {
                SetError("All fields are required, no values were updated");
                TempData["Errors"] = ModelState.Keys;
                return RedirectToAction("PluginConfig");
            }
        }

        [HttpGet]
        public ActionResult PluginConfig(string Plugin)
        {
            ViewData["Plugin"] = string.IsNullOrEmpty(Plugin) ? "" : Plugin;
            SetBreadCrumb("Plugins Config");
            IEnumerable<PluginConfig> AllPluginConfigs = Repositories.PluginConfigs.PluginConfigs();

            ViewData["Settings"] = AllPluginConfigs.Select(x => x.PluginGroup).Distinct().ToArray();

            return View(AllPluginConfigs);
        }

    }
}
